/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pagrafo.janela;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.util.ArrayList;
import pagrafo.controle.Controle;
import pagrafo.genetico.Roteamento;
import pagrafo.grafo.Grafo;
import pagrafo.grafo.Vertice;

/**
 *
 * @author Fabricio
 */
public class AreaDeDesenho extends javax.swing.JPanel {
    
    private double zoom;
    private Controle c;
    private Grafo grafo;
    private boolean modoDesenho;
    
    /**
     * Não Excluir !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     */
    public AreaDeDesenho(){
        
    }

    /**
     * Creates new form AreaDeDesenho2
     */
    public AreaDeDesenho(Controle c, boolean modoDesenho) {
        this.c = c;
        this.modoDesenho = modoDesenho;
        this.zoom = 6; // TODO Considerando o vertice mais distante em 100
        
        if(modoDesenho){
            this.zoom = 1;
            grafo = new Grafo();
            c.setGrafo(grafo);
        }
        else{
            grafo = c.getGrafo();
        }
        
        initComponents();
        this.setPreferredSize(new Dimension(600,600));
    }
    
    public void adicionarPonto(int x, int y, double demanda){
        if(modoDesenho){
            grafo.addVertice(x, y, demanda);
        }
    }
    
    @Override
    public void paint(Graphics g) {

        //Retangulo de fundo
        Polygon pol = new Polygon();
        pol.addPoint(0, 0);
        pol.addPoint(0, this.getHeight());
        pol.addPoint(this.getWidth(), this.getHeight());
        pol.addPoint(this.getWidth(), 0);    
        g.setColor(Color.white);
        g.fillPolygon(pol);  
        
        if(c.isTerminado()){
            g.setColor(Color.blue);
            int tamanhoCores = cores.length;
            int controleCor = 0;
            int[] rota = c.getRoteamento();
            for(int i=0; i<rota.length-1;i++){
                if(rota[i] == 0){
                    controleCor++;
                    if(controleCor == tamanhoCores){
                        controleCor = 0;
                    }
                    g.setColor(cores[controleCor]);
                }
                desenhaLinha(rota[i],rota[i+1],g);
            }
        }
        
        g.setColor(Color.blue);
        for (int i=1; i<grafo.sizeVertices();i++) {
            g.fillOval(getX(i)-4, getY(i)-4, 8, 8);
        }

        g.setColor(Color.red);
        if(grafo.sizeVertices() != 0)
            g.fillOval(getX(0)-8, getY(0)-8, 16, 16);
    }
    
    private int getX(int i){
        return (int)(grafo.getVertice(i).getX()*zoom);
    }
    
    private int getY(int i){
        return (int)(grafo.getVertice(i).getY()*zoom);
    }
    
    private void desenhaLinha(int a, int b, Graphics g){
        //Vertice i = grafo.getVertice(a);
        //Vertice f = grafo.getVertice(b);
        g.drawLine(getX(a),getY(a),getX(b),getY(b));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void setGrafo(Grafo grafo) {
        this.grafo = grafo;
    }

    public void setModoDesenho(boolean modoDesenho) {
        this.modoDesenho = modoDesenho;
    }

    public void setZoom(double zoom) {
        this.zoom = zoom;
    }
    
    private static final Color[] cores = new Color[]{Color.blue,Color.BLACK,Color.GREEN,Color.RED,Color.PINK,Color.MAGENTA,Color.YELLOW,Color.ORANGE};

    
}
